package com.defense.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;


import com.defense.util.ConnUtil;
import com.defense.util.MD5;

/**
* @类名  User.java
* @作者  jinbanglong
* @版本  V1.0
* @日期  2019年6月29日-下午2:45:57
* @描述     用户类：实现登录  注册
*
*/
public class User {
	private int uid;   				//id主键
	private String uName;			//用户名
	private String pwd;  			//正则验证、MD5加密
	private String str;  			//双重加密 字符串
	private String nickName;    	//昵称
	private String createTime;    	//创建时间
	private String lastLoginTime;	//最后一次登录时间
	private String status;			//状态
	
	private Connection conn = ConnUtil.getConnection();
	private PreparedStatement ps;
	private ResultSet RS;

	//注册
	
	public User register(User user) {
		user.str = MD5.rand();
		user.pwd = MD5.md5(MD5.md5(pwd)+user.str);
		user.createTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		user.lastLoginTime = user.createTime;
		user.status = "在线";
		try {
			int add = this.add(user);
			if(add>0) {
				return user;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	//登录
	public User login(String uName,String pwd) throws Exception  {
		
		String sql="select * from user where uname = ?";
		ps = conn.prepareStatement(sql);
		ps.setString(1, uName);
		RS = ps.executeQuery();
		User user= new User();
		if(RS.next()) {
			user.setUid(RS.getInt("uid"));
			user.setuName(RS.getString("uname"));
			user.setPwd(RS.getString("pwd"));
			user.setStr(RS.getString("str"));
			user.setNickName(RS.getString("nickname"));
			user.setCreateTime(RS.getString("createtime"));
			user.setLastLoginTime(RS.getString("lastlogintime"));
			user.setStatus(RS.getString("status"));
		}
		if((MD5.md5(MD5.md5(pwd)+user.getStr()).equals(user.pwd))) {
			//System.out.println(MD5.md5(MD5.md5(pwd)+user.getStr()));
			user.changeStatus(uName, "在线");
			return user;
		}
		
		return null;
	}
	
	//判断用户名是否已存在
	public boolean checkRepeat(String uName) {
		String sql="select uid from user where uname = ?";
		boolean b = false;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, uName);
			RS = ps.executeQuery();
			if(RS.next()) {
				b=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return b;
	}
	//改变用户状态
	public  void changeStatus(String uName,String status) {
		String sql="update user set status=? where uName=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, status);
			ps.setString(2, uName);
			int num = ps.executeUpdate();
//			if(num>0) {
//				System.out.println("已"+status);
//			}else {
//				System.out.println("失败");
//			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	//发送消息前验证用户有效性
	
	public boolean check(String uName) {
		String sql="select uid from user where uname = ? and status=?";
		boolean b = false;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, uName);
			ps.setString(2, "在线");
			RS = ps.executeQuery();
			if(RS.next()) {
				b=true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return b;
	}
	//添加
	public int add(User user) throws SQLException {
		String sql="insert into user (uname,pwd,str,nickname,createtime,lastlogintime,status) values (?,?,?,?,?,?,?)";
		ps = conn.prepareStatement(sql);
		ps.setString(1, user.uName);
		ps.setString(2, user.pwd);
		ps.setString(3, user.str);
		ps.setString(4, user.nickName);
		ps.setString(5, user.createTime);
		ps.setString(6, user.lastLoginTime);
		ps.setString(7, user.status);
		int num = ps.executeUpdate();
		return num;
//		if(num>0) {
//			System.out.println("插入成功");
//		}else {
//			System.out.println("插入失败");
//		}
	}
	public int update(User user) throws SQLException {
		String sql="update user set nickname=?,lastLoginTime=?,status=? where uid=?";
		ps = conn.prepareStatement(sql);
		ps.setString(1, user.nickName);
		ps.setString(2, user.lastLoginTime);
		ps.setString(3, user.status);
		ps.setInt(4, user.uid);
		int num = ps.executeUpdate();
		return num;
//		if(num>0) {
//			System.out.println("修改成功");
//		}else {
//			System.out.println("修改失败");
//		}
	}
	public int del(int sid) throws SQLException {
		String sql="delete from user where sid=?";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, sid);
		int num = ps.executeUpdate();
		return num;
//		if(num>0) {
//			System.out.println("删除成功");
//		}else {
//			System.out.println("删除失败");
//		}
	}
	
	public void select() throws SQLException {
		String sql="select * from user";
		ps = conn.prepareStatement(sql);
		RS = ps.executeQuery();
		
		while(RS.next()) {
			User user= new User();
			user.setUid(RS.getInt("uid"));
			user.setuName(RS.getString("uname"));
			user.setPwd(RS.getString("pwd"));
			user.setStr(RS.getString("srt"));
			user.setNickName(RS.getString("nickname"));
			user.setCreateTime(RS.getString("createtime"));
			user.setLastLoginTime(RS.getString("lastlogintime"));
			user.setStatus(RS.getString("status"));
			System.out.println(user);
		}
	}
	
	public User getInfo(int uid) throws SQLException {
		String sql="select * from user where uid = ?";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, uid);
		RS = ps.executeQuery();
		User user= new User();
		if(RS.next()) {
			user.setUid(RS.getInt("uid"));
			user.setuName(RS.getString("uname"));
			user.setPwd(RS.getString("pwd"));
			user.setStr(RS.getString("srt"));
			user.setNickName(RS.getString("nickname"));
			user.setCreateTime(RS.getString("createtime"));
			user.setLastLoginTime(RS.getString("lastlogintime"));
			user.setStatus(RS.getString("status"));
		}
		
		return user;
	}
	
	
	public User() {}

	public User(int uid, String uName, String pwd, String nickName, String lastLoginTime, String status) {
		this.uid = uid;
		this.uName = uName;
		this.pwd = pwd;
		this.nickName = nickName;
		this.lastLoginTime = lastLoginTime;
		this.status = status;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getuName() {
		return uName;
	}

	public void setuName(String uName) {
		this.uName = uName;
	}
	
	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(String lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	@Override
	public String toString() {
		return "User [uid=" + uid + ", uName=" + uName + ", pwd=" + pwd + ", str=" + str + ", nickName=" + nickName
				+ ", createTime=" + createTime + ", lastLoginTime=" + lastLoginTime + ", status=" + status+"]";
	}
	
	
}
